package team.jaman.autosell.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import team.jaman.autosell.dao.LoggerMapper;
import team.jaman.autosell.dao.ManagerMapper;
import team.jaman.autosell.entity.Logger;
import team.jaman.autosell.entity.Manager;
import team.jaman.autosell.entity.ManagerExample;
import team.jaman.autosell.service.ManagerService;
import team.jaman.autosell.tools.CommonData;
import team.jaman.autosell.tools.PageData;
@Service
public class ManagerServiceImpl implements ManagerService {
	// 注入一个BlogMapper
	@Autowired
	private ManagerMapper managerMapper;
	@Autowired
	private LoggerMapper loggerMapper;
	
	PageData<Manager> pageData;
	
	/**
	 * 根据账号密码查询管理员
	 */
	public Manager loginManager(Manager record,Logger logger) {
		
		Manager manager = managerMapper.loginManager(record);
		if (manager!=null) {
			logger.setManagerId(manager.getManagerId());
			logger.setOperatorContent("loginManager");
			logger.setErrorMsg("登录成功");
			logger.setEndTime(new Date());
			loggerMapper.insertSelective(logger);
			System.out.println(logger);
		}
		
		return manager;
	}
	/**
	 * 增加管理员
	 */
	public CommonData addManager(Manager record,Logger logger) {
		CommonData commonData = new CommonData();
		
		int n = managerMapper.insert(record);
		
		if (n>0) {
			logger.setManagerId(managerMapper.loginManager(record).getManagerId());
			logger.setOperatorContent("addManager");
			logger.setErrorMsg("添加成功");
			logger.setEndTime(new Date());
			loggerMapper.insertSelective(logger);
			System.out.println(logger);
			commonData.setMsg("添加成功");
		}else {
			commonData.setMsg("添加失败");
		}
		return commonData;
	}
	/**
	 * 查询管理员
	 */
	public PageData<Manager> selectManager(int page, int limit, String keyword) {
		
		ManagerExample example = new ManagerExample();
		// pageHelper分页插件
		Page<Manager> startPage = PageHelper.startPage(page, limit);
		// 模糊查询数据
		example.createCriteria().andManagerNameLike("%" + keyword + "%");
		List<Manager> data = managerMapper.selectByExample(example);
		// 总记录数
		long total = startPage.getTotal();
				
		PageData<Manager> pageData = new PageData<Manager>(data, (int) total, limit, page);
		pageData.setCode(0);
		pageData.setMsg("");
		return pageData;
	}
	/**
	 * 修改管理员
	 */
	public CommonData updateByPrimaryKeySelective(Manager record,Logger logger) {
		CommonData commonData = new CommonData();
		
		int n = managerMapper.updateByPrimaryKeySelective(record);

		if (n>0) {
			logger.setManagerId(record.getManagerId());
			logger.setOperatorContent("updateManager");
			logger.setErrorMsg("修改成功");
			logger.setEndTime(new Date());
			loggerMapper.insertSelective(logger);
			System.out.println(logger);
			commonData.setMsg("修改成功");
		}else {
			logger.setManagerId(record.getManagerId());
			logger.setOperatorContent("updateManager");
			logger.setErrorMsg("修改失败");
			logger.setEndTime(new Date());
			loggerMapper.insertSelective(logger);
			System.out.println(logger);
			commonData.setMsg("修改失败");
		}
		return commonData;
	}
	@Override
	public Manager selectManagerById(int managerId) {
		// TODO Auto-generated method stub
		return managerMapper.selectByPrimaryKey(managerId);
	}
}
